\begin{frame}{Generating Constraints}
%We are going to represent the constraints as a multiset of pairs \textbf{$(constraint, row)$} called \textbf{$\theta$}, where the constraints will be the conditions it's associated row have to satisfy to be in the result of the query.
%We are going to generate constraints over the symbolic database instance.\\
%This constraints are represented as a multiset of pairs of constraints and rows called $\theta$.\\

For each query or relation $R$ we define $\theta(R)$ as a multiset of pairs of constraints and rows. The constraints represents the conditions its associated row must satisfy to be in the selection result.

\begin{example}
{\small
$d$ is the symbolic database instance.\\

$d(T) = \multi{(T.A_0,T.B_0,T.C_0), (T.A_1,T.B_1,T.C_1)}$, (symbolic instance of the table $T$).\\

$Q = \cod{select \ A \ from \ T \ where \ A + B \ = \ C}$ \\
}
\textbf{$\theta(Q)= $\\$ \multileft((T.A_0 + T.B_0 = T.C_0) , (T.A_0)),$ $((T.A_1 + T.B_1 = T.C_1) , (T.A_1))\multiright$}
\end{example}

\begin{theorem}
Let $D$ be a database schema and $d_s$ a symbolic database instance.
Let $R$ be a relation in $D$
such that $\theta(R) = \multi{ (\psi_1, \mu_1), \dots (\psi_n,\mu_n) }$,
and $\eta$ a substitution satisfying  $d_s$.
Then $d_s\eta$ is a PTC for $R$ iff
$(\bigvee_{i=1}^n \psi_i)\eta = true$.
\end{theorem}

\end{frame}
\subsection{Tables}
\begin{frame}{Generating Constraints: Tables}

If \cod{T} is a table such that $d(T)= \multi{(\mu_1, \dots, \mu_n )}$without primary or foreign key constraints:
$$\theta(T)= \multi{(true,\mu_1),\dots, (true,\mu_n)}$$

We can see it like the query: ``\cod{select * from T}'', that would return all the rows of T.

\begin{example}
$d(T) = \multi{(T.A_0, T.B_0, T.C_0), (T.A_1, T.B_1, T.C_1)}$\\
$\theta(T) = \multi{(true, (T.A_0, T.B_0, T.C_0)),( true, (T.A_1, T.B_1, T.C_1))}$\\
And any substitution giving values to the logic variables would be a Test-Case
\end{example}

\end{frame}

\begin{frame}{Generating Constraints: Table}
 \label{def:constraints:primary} if $T$ has a primary key constraint for the columns $C_1, \dots, C_p $:
  Let $T'$ be the table $T$ without that primary key constraint.
  Assume that $\theta(T') = \multileft (\psi_1,\mu_1),$ $\dots,$ $(\psi_n,\mu_n) \multiright$, then:
$$\theta(T) = \multi{((\psi_i \wedge (\bigwedge_{j=1, j\neq i}^n(\bigvee_{k=1}^p \mu_i(T.C_k) \neq \mu_j(T.C_k) ))),\mu_i)|i \in 1,\dots,n}$$

\begin{example}create table T (A int, B int, C int, primary key (A, B));\\
$d(T) = \multi{(T.A_0, T.B_0, T.C_0), (T.A_1, T.B_1, T.C_1)}$\\
$\theta(T) = $\\
$\multileft(true \ and \ (T.A_0 \neq T.A_1 \ or \ T.B_0 \neq T.B_1)), (T.A_0, T.B_0, T.C_0)),$\\
$( true \ and \ (T.A_1 \neq T.A_0 \ or \ T.B_1 \neq T.B_0)), (T.A_1, T.B_1, T.C_1))\multiright$\\
 \end{example}

\end{frame}
\subsection{Queries}
\begin{frame}{Generating Constraints: Queries}

 If $Q$ is a query: ${\sf select } \ e_1, \dots, e_n \  {\sf from }\ R_1\ B_1 , \dots, R_m \ B_m\  \textrm{\sf where}\ C_w$;\\
Then:
\begin{description}
  \item[$\theta(Q)$]$ =  \multileft{ (\psi_1 \wedge \dots \wedge \psi_m \wedge \varphi (C_w(\mu)),(e_1(\mu), \dots, e_n(\mu)))\ \mid } $\\
 $ (\psi_1, \nu_1) \in \theta(R_1), \dots, (\psi_m, \nu_m) \in \theta(R_m),  \mu = {\nu_1}^{B_1} \odot \cdots \odot {\nu_m}^{B_m}   \multiright$
\end{description}

where\\

\begin{center}
{\small \begin{tabular}{c|c}
\textbf{Condition Type} & \textbf{$\varphi(C)$}\\
\hline
\textbf{false} & $\bot$ \\
\textbf{true} & $\top$ \\
\textbf{Arithmetic expression e} & $e$\\
\textbf{$e_1 \diamond e_2$} & $(\varphi(e_1) \diamond \varphi(e_2))$\\
\textbf{$ C_1\ \cod{ and }\ C_2$}& $ \varphi(C_1) \wedge \varphi(C_2)$  \\
\textbf{$ C_1\ \cod{ or }\ C_2$} & $ \varphi(C_1) \vee \varphi(C_2)$  \\
\textbf{not $C$}  & $\neg \varphi(C)$\\ 
\hline

\textbf{Exists $Q$} & \begin{tabular}{c} $(\vee_{j=1}^{p} \psi_j)$    with \\$\theta(Q) = \multileft (\psi_1, \mu_1),$\\$ \dots (\psi_{p},\mu_{p}) \multiright$.\\ \end{tabular}
\end{tabular}} 
\end{center}
\end{frame}

\begin{frame}{Generating Constraints: Queries}

 If $Q$ is a query: ${\sf select } \ e_1, \dots, e_n \  {\sf from }\ R_1\ B_1 , \dots, R_m \ B_m\  \textrm{\sf where}\ C_w$;\\
Then:
\begin{description}
  \item[$\theta(Q)$]$ =  \multileft{ (\psi_1 \wedge \dots \wedge \psi_m \wedge \varphi C_w(\mu),(e_1(\mu), \dots, e_n(\mu)))\ \mid } $\\
 $ (\psi_1, \nu_1) \in \theta(R_1), \dots, (\psi_m, \nu_m) \in \theta(R_m),  \mu = {\nu_1}^{B_1} \odot \cdots \odot {\nu_m}^{B_m}   \multiright$
\end{description}

 \begin{example}
   $T =$ \cod{create table T (A int, B int, C int);}\\
   $Q =$ \cod{select C from T where A + B = C;}\\
   $\theta(T) = \multi{(true, (T.A_0, T.B_0, T.C_0)),( true, (T.A_1, T.B_1, T.C_1))}$\\
   $\theta(Q) = \multileft ((true \ and \ (T.A_0 + T.B_0 = T.C_0)),T.C_0), $\\$(true \ and \ (T.A_1 + T.B_1 = T.C_1)),T.C_1)\multiright $
  \end{example}
\begin{center}
\end{center}
\end{frame}


\begin{frame}{Generating Constraints: Queries}

 If $Q$ is a query: ${\sf select } \ e_1, \dots, e_n \  {\sf from }\ R_1\ B_1 , \dots, R_m \ B_m\  \textrm{\sf where}\ C_w$;\\
Then:
\begin{description}
  \item[$\theta(Q)$]$ =  \multileft{ (\psi_1 \wedge \dots \wedge \psi_m \wedge \varphi C_w(\mu),(e_1(\mu), \dots, e_n(\mu)))\ \mid } $\\
 $ (\psi_1, \nu_1) \in \theta(R_1), \dots, (\psi_m, \nu_m) \in \theta(R_m),  \mu = {\nu_1}^{B_1} \odot \cdots \odot {\nu_m}^{B_m}   \multiright$
\end{description}

 \begin{example}
   $T =$ \cod{create table T (A int, B int, C int);}\\
   $Q =$ \cod{select C from T where A + B = C;}\\   
   $Q2 =$ \cod{select C from T where exists (Q);}\\
   $\theta(T) = \multi{(true, (T.A_0, T.B_0, T.C_0)),( true, (T.A_1, T.B_1, T.C_1))}$\\
   $\theta(Q) = \multileft ((true \ and \ (T.A_0 + T.B_0 = T.C_0)),T.C_0), $\\$(true \ and \ (T.A_1 + T.B_1 = T.C_1)),T.C_1)\multiright $
   $\theta(Q2) = \multileft (((T.A_0 + T.B_0 = T.C_0) \ or \ (T.A_1 + T.B_1 = T.C_1)),T.C_0),$\\$((T.A_0 + T.B_0 = T.C_0) \ or \ (T.A_1 + T.B_1 = T.C_1)),T.C_1)$
  \end{example}

$\theta$ is generated in similar way for views.
\end{frame}